
<?php 
class dbase{
	var $links ;
	var $result ;	
	function __construct($srv = 'localhost',$user='root',$password='1234',$database='fss')
	{
	// 连接，选择数据库
	//	echo "table name is:".$this->table.'<br />';
		$this->links = mysql_connect($srv, $user, $password) or die('Could not connect: ' . mysql_error());
	//	echo 'Connected successfully <br />';
		mysql_select_db($database) or die('Could not select database');
		mysql_query("set names utf8;"); //PHP 连接数据库指定UTF-8编码
	}
	
	function getdata($condition="`status` = 'disable'" ,$table='tb_files')
	{
		$query = "SELECT * FROM `".($table)."` WHERE ".$condition;
		$this->result = mysql_query($query) or die('Query failed: ' . mysql_error());
		$i=0;
		while($line = mysql_fetch_array($this->result, MYSQL_ASSOC))
		{
			$ret[$i] = $line;
			$i++;
		}
		return $ret;		
	}

	function insert($array,$table='tb_files')
	{
		 foreach($array as $k => $v){
			$fields.="`".$k."`,";
			$value.="'".$v."',";	
		 }
		 $fields= substr($fields,0,-1);
		 $value= substr($value,0,-1);
		 $str="INSERT INTO `".$table."` ( ".$fields." )VALUES (".$value.");";
		//die($str);
		mysql_query($str) or die('Query failed: ' . mysql_error());
	}
	
	function update($id,$array,$table='tb_files')
	{
		/*UPDATE `tb_files` SET `url` = 'ADFADa', `clicks` = '232' WHERE `tb_files`.`id` = 10 LIMIT 1;*/
		foreach($array as $k => $v)
		{
			$data.="`".$k."`= '".$v."',";	
		 }	 
		$data = substr($data,0,-1);	
		$str = "UPDATE `".$table."` SET".$data."WHERE `id` = ".$id." ;";
		//die($str);
		mysql_query($str) or die('update failed: ' . mysql_error());
	}

	function delete($id, $table='tb_files')
	{	
		$str = "DELETE FROM `".$table."` WHERE `id` =".$id;
		mysql_query($str) or die('delete failed: ' . mysql_error());	
	}
	
	function  __destruct() 
	{
		// 释放结果集
		if($this->result)
			mysql_free_result($this->result);	
		// 关闭连接
		mysql_close($this->links);	
		//echo "\n Destruct successfully!";
	}
}

// 应用例子
//	$db = new dbase();
////****************查询*********************
//
//
///***************插入************************
///*[id] => NULL [file_name] => StdAfx.h [extension] => h [url] => ./files/20110715192235.h [clicks] => 0 [date_time] => 2011-07-15 19:22:35 [status] => active [version] => 1 [owner] => unknown */
//echo get_magic_quotes_gpc();
//
//$array['id'] = 'NULL';
//$array['file_name'] = 'StdAfx.h';
//$array['extension'] = 'h';
//$array['url'] = './files/20110715192235.h';
//$array['classify'] = '0';
//$array['clicks'] = '0';
//$array['date_time'] = '2011-07-19 15:46:06';
//$array['status'] = addslashes("dis'able");
//$array['version'] = '1';
//$array['owner'] = 'sam';
//$db->insert($array);
//
//	$arr=$db->getdata("`owner` = 'sam'");
//	foreach ($arr as $arr2)
//	{
//	print_r("<table><tr>");
//	foreach ($arr2 as $k=>$v)
//	{
//	print_r("<td >  ".$v."   </td>");
//	}
//	print_r("</tr></table>");
//	}
//print_r($array);
//
/**************更新**********************
//	$array['file_name'] = 'KEYBOARD';
//	$array['url'] = 'SZS.ST.COM';
//	$db->update(11,$array);
*/
	
?>
